package util;

public class BarCode {
    private int number;
    private final static int[] validCodes = { 5, 9, 13, 15, 19, 25, 37, 55 };

    public BarCode(int number) {
        this.number = number;
        if (!isValidCode(number))
        	this.number = calculateInverseNumber(number);
    }

    public static int calculateInverseNumber(int number) {
    	int rest = number;
		int result = 0;
		for(int i = 5; i >=0; i--)
		{
			int bit = (int) (rest / Math.pow(2, i));
			rest -= bit*Math.pow(2, i); 
			result += (int) (Math.pow(2, (5-i))*bit);
//			System.out.println(bit);
		}
		return result;
    }
    
    private static boolean isValidCode(int number)
    {
    	for (int i = 0; i < validCodes.length; i++)
            if (validCodes[i] == number)
                return true;
    	return false;
    }

    public int getNumber() 
    {
    	return this.number;
    }
    
    
}
